package com.naiterui.ehp.bp.security.utils;

import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import java.security.PublicKey;

import javax.crypto.Cipher;

/**
 * Rsa 工具类，公钥私钥生成，加解密
 **/
@SuppressWarnings("unused")
public class RsaUtils {

    /**
     * 公钥解密
     *
     * @param publicKey 公钥
     * @param text      待解密的信息
     *
     * @return /
     */
    public static String decryptByPublicKey(String publicKey, String text) {
        RSA rsa = new RSA(null, publicKey);
        return rsa.decryptStr(text, KeyType.PublicKey);
    }

    /**
     * 私钥解密
     *
     * @param privateKey 私钥
     * @param text       待解密的文本
     *
     * @return /
     */
    public static String decryptByPrivateKey(String privateKey, String text) {
        RSA rsa = new RSA(privateKey, null);
        return new String(rsa.decrypt(text, KeyType.PrivateKey));
    }

    /**
     * 公钥加密
     *
     * @param text
     *
     * @return
     *
     * @throws Exception
     */
    public static String encryptByPrivateKey(String privateKey, String text) throws Exception {
        RSA rsa = new RSA(null, privateKey);
        return rsa.encryptStr(text, KeyType.PrivateKey);
    }
}
